"""管理员模型"""
# 字段类型引入 column列
from sqlalchemy import Column, Integer, String, DateTime, func, text
from sqlalchemy.orm import relationship

from soulapi.db import Base, engine


# 基于BaseNoel定义请求体的结构（json对象）
class Admin(Base):
    __tablename__ = 'admin'  # 数据表表名，__tablename__：设置映射到的表名不设置，默认映射到类名post下
    __table_args__ = {'comment': '管理员'}
    ad_id = Column(Integer, primary_key=True, index=True, autoincrement=True, server_default=text("0"), comment='管理员ID') # index索引设置,autoincremen自增长
    ad_name = Column(String(25), unique=True, nullable=False, server_default=text("''"), comment='真实姓名')  # 唯一、不为空,comment添加字节
    adminname = Column(String(30), unique=True, nullable=False, server_default=text("''"), comment='登陆账号/手机号')
    password = Column(String(128), nullable=False, server_default=text("''"), comment='登陆密码')
    # 关系(back_populates双向显示)
    news = relationship('News', back_populates='admin')
    test = relationship('Test', back_populates='admin')
    notic = relationship('Notic', back_populates='admin')
    # 时间获取
    created_at = Column(DateTime, server_default=func.now(), comment='创建时间')
    updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now(), comment='更新时间')


# 在数据库中生成表
Base.metadata.create_all(engine)